/* eslint-disable no-unused-vars */
import Vue from "vue";
import VueI18n from 'vue-i18n'
import {message, t} from '@/locale'
import {Button, Input, Select, Option, Form, FormItem} from "element-ui";
import "@/assets/scss/common.scss"
import main from "@/jsSDK/main/index.vue"
import request from "@/utils/request";
import getEnv from "@/utils/env";

Vue.use(VueI18n)
Vue.use(Button);
Vue.use(Input);
Vue.use(Select);
Vue.use(Option);
Vue.use(Form);
Vue.use(FormItem);
Vue.prototype.$tt = t

class jsSDK {
  constructor(options, callback) {
    if (options.app_key === "A0002") {
      jsSDK.render(options, callback);
      jsSDK.instance = this;
    } else {
      if (!jsSDK.instance) {
        jsSDK.render(options, callback);
        jsSDK.instance = this;
      }
    }
  }

  static render(options, callback) {
    // 默认配置
    const opt = {
      mode: 'master',
      el: "#app",
      // 语言
      language: 'en-us',
      // tokenName
      tokenName: 'UNI_LOGIN_TOKEN',
      // app_key: 应用key WEB端传A0001, 客户端传A0002
      app_key: 'A0001',
      // platform: 网页平台 pc/mobile
      platform: 'pc',
      // scene: 场景值：website 网站、member 会员中心、client 游戏客户端
      scene: 'website',
      // from: 广告位
      from: '',
      // 大渠道id
      channel_id: '',
      // 子渠道
      sub_channel_id: '',
      // 广告位ID
      channel_ad_id: '',
      // 邀请码
      share_code: '',
      // 是否显示下载
      show_download: false,
      // params (token)
      // loginSuccess: Function
    }
    this.options = {...opt, ...options};
    // 设置请求接口BaseURL
    request.defaults.baseURL = getEnv(options.mode).apiHost
    // 设置国际化
    const i18n = new VueI18n({
      local: options.language, // 设置语言
      message: message // 语言包
    })
    window._language = options.language
    // console.log(options.language)
    const el = this.options.el;
    // console.log(el, '---el----');
//     el: String,
//         mode: {type: String, default: 'master'},
// // 网页平台 pc/mobile
//     platform: {type: String, default: 'pc'},
// // WEB端传A0001, 客户端传A0002
//     app_key: {type: String, default: 'A0001'},
// // 场景值：website 网站、member 会员中心、cilent 游戏客户端
//     scene: {type: String, default: 'website'},
// // 广告位
//     from: {type: String, default: ''},
// // 大渠道id
//     channel_id: {type: String, default: ''},
// // 子渠道
//     sub_channel_id: {type: String, default: ''},
// // 广告位ID
//     channel_ad_id: {type: String, default: ''},
//     appId: [String, Number],
// // 登录成功
//         loginSuccess: {type: Function},
// // 注册成功
//     registerSuccess: {type: Function}
// // 调用三方登录
//     thirdLogin: {type: Function}
    console.log('> loginSDK config:')
    console.log(this.options)
    this.insVue = new Vue({
      i18n,
      render: (h) => h(main, {props: this.options}),
      mode: options.mode ? options.mode : 'master', // 在组件中可通过this.$root.$options.mode进行访问
      props: {
        ...this.options
      },
      mounted() {
        callback && callback();
      }
    }).$mount(el);
  }

  static init(options, callback) {

    console.log('> NODE_ENV: ', process.env.NODE_ENV);
    console.log('> VUE_APP_SERVER_ENV: ', process.env.VUE_APP_SERVER_ENV);
    console.log('> VUE_APP_RECAPTCHA_ID: ', process.env.VUE_APP_RECAPTCHA_ID);
    // console.log(options, '----options---');

    return new jsSDK(options, callback);
  }

  static destroy() {
    jsSDK.instance = null;
    this.insVue.$el.innerHTML = '';
    if (this.insVue && this.insVue.$destroy) {
      this.insVue.$destroy();
    }
  }
}


export default jsSDK;
